package com.eonmind.user.mapper;

import com.eonmind.common.contract.data.base.BaseMongoMapper;
import com.eonmind.common.contract.enums.DelEnum;
import com.eonmind.common.contract.enums.user.NoticeTypeEnum;
import com.eonmind.common.provider.entity.user.UserNotification;
import com.eonmind.user.contract.dto.UserInfoData;
import com.eonmind.user.dto.UserNoticeDto;
import com.mongoplus.conditions.query.QueryWrapper;
import com.mongoplus.conditions.update.UpdateWrapper;
import com.mongoplus.model.PageResult;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Xujie
 * @since 2025/6/1 11:11
 **/

@Component
public class UserNotificationMapper extends BaseMongoMapper<UserNotification> {
    public PageResult<UserNotification> pageUserNotice(UserInfoData userInfoData, UserNoticeDto.UserNoticePageRequest userNoticePageRequest) {
        QueryWrapper<UserNotification> between = createMgoQuery()
                .eq(UserNotification::getUserId, userInfoData.getId())
                .eq(UserNotification::getNoticeNotifyType, NoticeTypeEnum.IN_SITE.getCode())
                .eq(ObjectUtils.isNotEmpty(userNoticePageRequest.getStatus()), UserNotification::getStatus, userNoticePageRequest.getStatus())
                .eq(ObjectUtils.isNotEmpty(userNoticePageRequest.getNoticeTypeId()), UserNotification::getNoticeTypeId, userNoticePageRequest.getNoticeTypeId())
                .between(ObjectUtils.allNotNull(userNoticePageRequest.getStartTime(), userNoticePageRequest.getEndTime()) && userNoticePageRequest.getStartTime() < userNoticePageRequest.getEndTime()
                        , UserNotification::getCreateTime, userNoticePageRequest.getStartTime()
                        , userNoticePageRequest.getEndTime()
                        , false);
        return page(between, userNoticePageRequest.getPage());


    }

    public void deleteByIds(Long id, List<Long> ids) {
        if (ObjectUtils.isEmpty(id)) return;
        UpdateWrapper<UserNotification> set = createMgoUpdate()
                .eq(UserNotification::getUserId, id)
                .in(UserNotification::getId, ids)
                .eq(UserNotification::getDel, DelEnum.UN_DEL.getCode())
                .set(UserNotification::getDel, DelEnum.DEL.getCode());
        update(set);
    }

    public void updateUserNoticeStatus(Long id, List<Long> ids, Integer code) {
        if (ObjectUtils.isEmpty(id)) return;

        UpdateWrapper<UserNotification> set = createMgoUpdate()
                .eq(UserNotification::getUserId, id)
                .in(UserNotification::getId, ids)
                .eq(UserNotification::getDel, DelEnum.UN_DEL.getCode())
                .set(UserNotification::getStatus, code);
        update(set);
    }
}
